package com.sand.util;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.junit.Test;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Random;
import java.util.Set;

public class YZMIcon {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/10/17/017 19:24
 */
//验证码
public void getCode(HttpServletRequest request, HttpServletResponse response)throws Exception{

    //创建底图
    ImageIcon imageIcon = new ImageIcon(request.getServletContext().getRealPath("layui/images/bg.jpg"));
    //System.out.println(imageIcon.getIconHeight()+"||"+imageIcon.getIconWidth());
    //创建画布|宽|高|背景样式
    BufferedImage bufferedImage = new BufferedImage(100,46,BufferedImage.TYPE_4BYTE_ABGR);
    //创建画笔
    Graphics graphics = bufferedImage.getGraphics();
    //画背景图片|图片|x起始位置|y起始位置|是否覆盖
    graphics.drawImage(imageIcon.getImage(),0,0,null);
    //创建内容
    String str="abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    //创建随机数对象
    Random rd = new Random();
    //获得随机数
    StringBuffer strCode = new StringBuffer();
    for (int i = 0; i < 4; i++) {
        strCode.append(str.charAt(rd.nextInt(str.length())));
    }
    //将验证码存入session
    HttpSession session = request.getSession();
    session.setAttribute("code",strCode);
    session.setMaxInactiveInterval(30);
    //设置画笔颜色
    graphics.setColor(Color.black);
    //设置画笔字体
    graphics.setFont(new Font("楷体",Font.BOLD,35));
    //画字符|字符|x起始位置|y起始位置
    graphics.drawString(strCode.toString(),10,40);
    //设置线条
    for (int i = 0; i < 10; i++) {
        graphics.setColor(Color.red);
        int x1=rd.nextInt(imageIcon.getIconWidth());
        int y1=rd.nextInt(imageIcon.getIconHeight());

        int x2=rd.nextInt(imageIcon.getIconWidth());
        int y2=rd.nextInt(imageIcon.getIconHeight());
        //将两个点连接起来
        graphics.drawLine(x1,y1,x2,y2);
    }
    //输出图片|输出内容|输出格式|输入方式
    ImageIO.write(bufferedImage,"jpg",response.getOutputStream());
}

    public static String getPhoneCode(String phone){
        //生产环境请求地址：app.cloopen.com
        String serverIp = "app.cloopen.com";
        //请求端口
        String serverPort = "8883";
        //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        String accountSId = "8a216da87d55f9cc017d6ae382b9047f";
        String accountToken = "7f86e2359ec64f1aa1e80e75436a1c95";
        //请使用管理控制台中已创建应用的APPID
        String appId = "8a216da87d55f9cc017d6ae383d20485";
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();

        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        //生成随机6位验证码
        int yzm = (int)((Math.random()*9+1)*100000);
        String str=String.valueOf(yzm);
        String to="17382243254";
        //发送验证码/手机号/发送模板：1/发送信息/推送验证码有效时间
        HashMap<String, Object> result = sdk.sendTemplateSMS(phone, "1", new String[]{str, "1"});
        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
        }
        return str;
    }
}
